import util from 'util.js';


/**
 * url 请求地址
 * success 成功的回调
 * fail 失败的回调
 */
function _get(url, success, fail) {

  console.log("------start---_get----");
  wx.request({
    url: url,
    header: {
      // 'Content-Type': 'application/json'
    },
    success: function (res) {
      success(res);
    },
    fail: function (res) {
      fail(res);
    }
  });

  console.log("----end-----_get----");
}

/**
 * url 请求地址
 * success 成功的回调
 * fail 失败的回调
 */
function _post(url, data, success, fail) {
  console.log("----_post--start-------");
  wx.request({
    url: url,
    header: {
      'content-type': 'application/x-www-form-urlencoded',
    },
    method: 'POST',
    data: { data: data },
    success: function (res) {
      success(res);
    },
    fail: function (res) {
      fail(res);
    }
  });
  console.log("----end-----_get----");
}


/**
 * url 请求地址
 * success 成功的回调
 * fail 失败的回调
 */
function _post1(that, url, data, success, fail) {
  wx.showLoading({
    title: '',
  })
  wx.request({
    url: url,
    header: {
      'content-type': 'application/x-www-form-urlencoded',
    },
    method: 'POST',
    data: data,
    success: function (res) {
      success(that, res);
    },
    fail: function (res) {
      fail(that, res);
    },
    complete: function () {
      wx.hideLoading();
    }
  });
}

/**
 * 错误日志上传
 */
function _error(url, data) {
  wx.showLoading({
    title: '',
  })
  console.log(data)
  wx.request({
    url: url,
    header: {
      'content-type': 'application/x-www-form-urlencoded',
    },
    method: 'POST',
    data: data,
    success: function (res) {
      console.log(res)
    },
    fail: function (res) {
      console.log(res)
    },
    complete: function () {
      wx.hideLoading();
    }
  });
}

/**
* url 请求地址
* success 成功的回调
* fail 失败的回调
*/
function _post_json(that, url, data, success) {
  _post_page_json(that, url, 0, data, success)
}


/**
* 可分页
* url 请求地址
* success 成功的回调
* fail 失败的回调
*/
function _post_page_json(that, url, start, data, success) {

  // data.userId = "cYkC00eE4pA9GdDQamOwY6";
  // data.tenantId = "eccnJuWxk-H8bpbS+FcBwH";
  // // data.openId = "o9EMh0QEmgqTCmGfzSUrtHcMi6R8";
  // data.appId = "wx23f00e7babb692b5";
  // // //wxeaf7672d43934e24
  // _post_request(that, url, start, data, success);


  var app = getApp();
  if (app.globalData.openId) {
    data.openId = app.globalData.openId;
    data.userId = app.globalData.userId;
    data.tenantId = app.globalData.tenantId;
    _post_request(that, url, start, data, success);
  } else {
    // 由于 openId 是网络请求，可能会在 Page.onLoad 之后才返回
    // 所以此处加入 callback 以防止这种情况
    app.userAndTenantInfoCallback = res => {
      data.openId = app.globalData.openId;
      data.userId = res.id;
      data.tenantId = res.tenant.id;
      _post_request(that, url, start, data, success);
    }
  }
}

/**
* 可分页
* that 页面指针
* url 请求地址
* start 从多少条数据开始求请
* data 求请参数
* success 成功的回调
*/
function _post_request(that, url, start, data, success) {
  console.log(url);
  console.log(JSON.stringify(data));
  wx.showLoading({
    title: '',
  })
  wx.request({
    url: url + "?start=" + start + "&length=20",
    // header: {
    //     'content-type': 'application/json',
    // },
    method: 'POST',
    data: data,
    success: function (res) {
      console.log(res.data);
      if (res.data.status == 1) {
        success(that, res);
      } else if (res.data.status == 2) {
        if (res.data.data) {
          that.showZanTopTips(res.data.data);
        } else {
          that.showZanTopTips(res.data.message);
        }
        // if (res.data.message == '第一个地址只能是默认地址') {
        //   that.showZanTopTips('必须保留一个默认地址');
        // } else
        //   if (res.data.message == 'MOBILE_IN_BLACKLIST') {
        //     wx.showToast({
        //       title: '手机号被占用',
        //       image: "../../images/no_data.png",
        //       duration: 2000
        //     })
        //   } else {
        //     wx.showToast({
        //       title: res.data.message,
        //       image: "../../images/no_data.png",
        //       duration: 2000
        //     })
        //   }
      } else {
        that.showZanTopTips('未找到服务');
      }
    },
    fail: function (res) {
      that.showZanTopTips('网络连接出错');
    },
    complete: function () {
      wx.hideLoading();
    },
  });
}

function _post_product_request(that, url, start, orderType, order, data, success) {

  // data.userId = "cYkC00eE4pA9GdDQamOwY6";
  // data.tenantId = "eccnJuWxk-H8bpbS+FcBwH";
  // // data.openId = "o9EMh0QEmgqTCmGfzSUrtHcMi6R8";
  // data.appId = "wx23f00e7babb692b5";
  // // //wxeaf7672d43934e24
  // post_product_request(that, url, start, orderType, order, data, success);


  var app = getApp();
  if (app.globalData.openId) {
    data.openId = app.globalData.openId;
    data.userId = app.globalData.userId;
    data.tenantId = app.globalData.tenantId;
    post_product_request(that, url, start, orderType, order, data, success);
  } else {
    // 由于 openId 是网络请求，可能会在 Page.onLoad 之后才返回
    // 所以此处加入 callback 以防止这种情况
    app.userAndTenantInfoCallback = res => {
      data.openId = app.globalData.openId;
      data.userId = res.id;
      data.tenantId = res.tenant.id;
      post_product_request(that, url, start, orderType, order, data, success);
    }
  }
}

/**
* 商品
* that 页面指针
* url 请求地址
* start 从多少条数据开始求请
* data 求请参数
* success 成功的回调
*/
function post_product_request(that, url, start, orderType, order, data, success) {
  console.log(url);
  console.log(JSON.stringify(data));

  wx.showLoading({
    title: '',
  })
  wx.request({
    url: url + "?start=" + start + "&length=20" + '&order[0][column]=1&order[0][dir]=' + orderType + '&columns[1][data]=' + order,
    method: 'POST',
    data: data,
    success: function (res) {
      console.log(res.data);
      if (res.data.status == 1) {
        success(that, res);
      } else {
        that.showZanTopTips(res.data);
      }
    },
    fail: function (res) {
      that.showZanTopTips(res.data);
    },
    complete: function () {
      wx.hideLoading();
    },
  });
}

module.exports = {
  _get: _get,
  _post: _post,
  _post1: _post1,
  _post_json: _post_json,
  _post_page_json: _post_page_json,
  _post_product_request: _post_product_request,
  _error: _error
}
